package org.blog.com.spring_blog.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.beans.Encoder;
import java.security.Key;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

public class JwtUtils {

    private static final long JWT_EXPIRATION = 60*60*1000;
    private static final String secreKey="ylgCzwbri0M/UIxOhCR9O1yA+m+meUHpfSJa7AVZoOg=";
    private static final Key key=  Keys.hmacShaKeyFor(Decoders.BASE64.decode(secreKey));
    /*
   生成令牌
    */
    public static String getToken(Map<String, Objects> map){
        String token = Jwts.builder().setClaims(map).setExpiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION))
                .signWith(key)
                .compact();
        return token;
    }

    /*
    令牌校验
     */
    public static Claims parseToken(String token){
        JwtParser build = Jwts.parserBuilder().build();
        Claims claims=null;
        try {
            claims=build.parseClaimsJws(token).getBody();
        }catch (Exception e){
            if (claims==null){
                return null;
            }

        }
        return claims;
    }

    public static Integer getIdByToken(String token){

    }

}
